<?php

namespace App\Http\Controllers\Administrator\Game;

use Illuminate\Http\Request;
use Dingo\Api\Routing\Helpers;
use App\Models\Def\MainGamePlat;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use App\Http\Requests\Administrator\MainGamePlatRequest;

class MainGamePlatController extends Controller
{
    use Helpers;

    /**
     * gameapi
     * 2018/10/8 15:03
     * Administrator
     * index
     * Display a listing of the resource.
     * @param MainGamePlat $mainGamePlat
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function index(MainGamePlat $mainGamePlat)
    {
        $this->authorize('index', $mainGamePlat);
        $plats = MainGamePlat::paginate(config('administrator.paginate.limit'));
        return view('backend.game.main_game_plat.index', compact('plats'));
    }

    /**
     * gameapi
     * 2018/10/8 15:03
     * Administrator
     * create
     * Show the form for creating a new resource.
     * @param MainGamePlat $mainGamePlat
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function create(MainGamePlat $mainGamePlat)
    {
        $this->authorize('create', $mainGamePlat);
        return view('backend.game.main_game_plat.create_and_edit', ['plat' => $mainGamePlat]);
    }

    /**
     * gameapi
     * 2018/10/8 15:03
     * Administrator
     * store
     * Store a newly created resource in storage.
     * @param MainGamePlatRequest $request
     * @param MainGamePlat $mainGamePlat
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function store(MainGamePlatRequest $request, MainGamePlat $mainGamePlat)
    {
        $this->authorize('create', $mainGamePlat);
        $data = $request->only(['main_game_plat_name', 'main_game_plat_code', 'status', 'rate']);
        MainGamePlat::create($data);
        return redirect()->route('main_game_plat.index')->with('success', '添加成功.');
    }

    /**
     * gameapi
     * 2018/10/6 11:00
     * Administrator
     * show
     * Display the specified resource.
     * @param $id
     */
    public function show($id)
    {
        //
    }

    /**
     * gameapi
     * 2018/10/8 15:03
     * Administrator
     * edit
     * Show the form for editing the specified resource.
     * @param MainGamePlat $mainGamePlat
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function edit(MainGamePlat $mainGamePlat)
    {
        $this->authorize('update', $mainGamePlat);
        return view('backend.game.main_game_plat.create_and_edit', ['plat' => $mainGamePlat]);
    }

    /**
     * gameapi
     * 2018/10/8 15:04
     * Administrator
     * update
     * Update the specified resource in storage.
     * @param MainGamePlatRequest $request
     * @param MainGamePlat $mainGamePlat
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function update(MainGamePlatRequest $request, MainGamePlat $mainGamePlat)
    {
        $this->authorize('update', $mainGamePlat);
        $result = $mainGamePlat->update($request->only(['status', 'rate']));

        if (!empty($result)) {
            return redirect()->route('main_game_plat.index')->with('success', '修改成功.');
        }
    }

    /**
     * gameapi
     * 2018/10/8 15:04
     * Administrator
     * destroy
     * Remove the specified resource from storage.
     * @param MainGamePlat $mainGamePlat
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Exception
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function destroy(MainGamePlat $mainGamePlat)
    {
        $this->authorize('destroy', $mainGamePlat);
        $mainGamePlat->delete();
        return redirect()->route('main_game_plat.index')->with('success', '删除成功.');
    }

    /**
     * gameapi
     * 2018/10/8 15:04
     * Administrator
     * batchDestroy
     * 批量删除
     * @param Request $request
     * @param MainGamePlat $mainGamePlat
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function batchDestroy(Request $request, MainGamePlat $mainGamePlat)
    {
        $this->authorize('destroy', $mainGamePlat);
        $ids = json_decode($request->getContent(), true);

        if (!empty($ids)) {
            DB::beginTransaction();
            $result = MainGamePlat::whereIn('id', $ids)->delete();
            if ($result) {
                DB::commit();
                return $this->response->array([
                        'status_code' => 200,
                        'message' => '删除成功！受影响行数：' . $result,
                    ]
                );
            } else {
                DB::rollBack();
                return $this->response->error('删除失败！受影响行数为0！', 500);
            }
        }
    }
}
